package liangtWorkSpace.proxy.subject.House;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName BuyHourseCglib
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/13 17:52
 */
public class BuyHourseCglib implements MethodInterceptor {
    private Object target;

    public Object createProxy(Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer();

        //设置代理目标
        enhancer.setSuperclass(this.target.getClass());
        //设置单一回调对象，在调用中拦截对目标方法的调用
        enhancer.setCallback(this);
        //设置类加载器
        enhancer.setClassLoader(this.target.getClass().getClassLoader());
        return enhancer.create();
    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object result;
        System.out.println("买房前准备");
        result = methodProxy.invokeSuper(o, objects);
        System.out.println("买房后装修");
        return result;
    }
}
